/*
Copyright 2009 David Revell

This file is part of SwiFTP.

SwiFTP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SwiFTP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SwiFTP.  If not, see <http://www.gnu.org/licenses/>.
*/

package lfr.ustc.ftp;

import android.content.SharedPreferences;

public class CmdPASS extends CmdManager implements Runnable {
	String input;
	String password;
	String username;
	boolean needCustomPwd;
	SharedPreferences settings;

	public CmdPASS(SocketManager sessionThread, String input) {
		super(sessionThread);
		this.input = input;
	}

	public void run() {
		String attemptPassword = getParameter(input);
		String attemptUsername = sessionThread.getUserName();
		if (attemptUsername == null) {
			sessionThread.sendInfo("503 �û�����Ϊ�� in CommandPASS\r\n");
		}
		settings = CommonSettings.getSettings();
		username = settings.getString("username", "");
		password = settings.getString("pwd", "");
		needCustomPwd = settings.getBoolean("needpwd",
				false);
		if (username == null || password == null) {
			sessionThread.sendInfo("500 Internal error during authentication in CommandPASS");
		} else if ("anonymous".equals(attemptUsername) 
				&& !needCustomPwd) {
			sessionThread.sendInfo("230 ��֤ͨ��\r\n");
			sessionThread.isLogin = true;
		} else if (username.equals(attemptUsername)
				&& password.equals(attemptPassword) ) {
			sessionThread.sendInfo("230 ��֤ͨ��\r\n");
			sessionThread.isLogin = true;
		}  else {
			sessionThread.sendInfo("530 �û�����������. in CommandPASS\r\n");
			sessionThread.isLogin = true;
		}
	}
}
